{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b6ff313",
   "metadata": {},
   "source": [
    "## 通用文字识别（高精度版）\n",
    "* 接口描述\n",
    "> 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。\n",
    "* 请求URL\n",
    "> https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d3028497",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和 url/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，要求base64编码和urlencode...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和 image/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pdf_file</td>\n",
       "      <td>和 image/url 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>pdf_file_num</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>language_type</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>auto_detect CHN_ENGENGJAPKORFRESPAPORGERITARUS...</td>\n",
       "      <td>识别语言类型，默认为CHN_ENG可选值包括：- auto_detect：自动检测语言，并识...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>detect_direction</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>paragraph</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否输出段落信息</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>probability</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否返回识别结果中每一行的置信度</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 参数                  是否必选      类型  \\\n",
       "0             image    和 url/pdf_file 三选一  string   \n",
       "1               url  和 image/pdf_file 三选一  string   \n",
       "2          pdf_file       和 image/url 三选一  string   \n",
       "3      pdf_file_num                     否  string   \n",
       "4     language_type                     否  string   \n",
       "5  detect_direction                     否  string   \n",
       "6         paragraph                     否  string   \n",
       "7       probability                     否  string   \n",
       "\n",
       "                                               可选值范围  \\\n",
       "0                                                  -   \n",
       "1                                                  -   \n",
       "2                                                  -   \n",
       "3                                                  -   \n",
       "4  auto_detect CHN_ENGENGJAPKORFRESPAPORGERITARUS...   \n",
       "5                                         true/false   \n",
       "6                                         true/false   \n",
       "7                                         true/false   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，要求base64编码和urlencode...  \n",
       "1  图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过1...  \n",
       "2  PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...  \n",
       "3  需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...  \n",
       "4  识别语言类型，默认为CHN_ENG可选值包括：- auto_detect：自动检测语言，并识...  \n",
       "5  是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...  \n",
       "6                                           是否输出段落信息  \n",
       "7                                   是否返回识别结果中每一行的置信度  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/1k3h7y3db')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d950fb48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.f95442af65cd69ec3b68c11412bf8e13.315360000.1965040176.282335-25946377', 'expires_in': 2592000, 'session_key': '9mzdDFFMeJ9ticMUDUeUOdYLg4VmBR232ck5feA3tTbBbG0fwN8jxBEdKXqz1EobmUJTxwkmEmPZZXwFWUzcSkpLu97SPg==', 'access_token': '24.7d66189b0a36ef6d6e62e8ad5d4a111a.2592000.1652272176.282335-25946377', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'cdda2c1ad8543efc948bd52461645196'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'jyEwNxuiemY2wt0HzYa6cUYs',\n",
    "    'client_secret':'YC8PkxoMQG6v17kDRI4K6w7HUypLZE8T'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "095e6b59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.e418098fdb42b186adb30489ff75ff3a.315360000.1965040221.282335-25946377', 'expires_in': 2592000, 'session_key': '9mzdD0HyX0DYzXSvvZAJjvvi6/N7C8iFRyRnpxnahOqsdJKwMRPcCvsvc8llWeozFn3Yd2GWj9VLSnJ3ICSEljiyHR4m7w==', 'access_token': '24.4e2271b5e85e8ee5b5781d69dcd5d509.2592000.1652272221.282335-25946377', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '2959d9a487661e5e9b8d5759af09b03c'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=jyEwNxuiemY2wt0HzYa6cUYs&client_secret=YC8PkxoMQG6v17kDRI4K6w7HUypLZE8T'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "adb0988b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.4e2271b5e85e8ee5b5781d69dcd5d509.2592000.1652272221.282335-25946377'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "c51c9e80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '桃花依旧笑春风'}, {'words': '人面不知何处去'}, {'words': '人面桃花相映红'}, {'words': '去年今日此门中'}, {'words': '崔护'}, {'words': '题都城南庄'}], 'words_result_num': 6, 'log_id': 1513494699262208353}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('诗歌.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "941c3d79",
   "metadata": {},
   "source": [
    "## 网络图片文字识别\n",
    "* 接口描述\n",
    "> 针对网络图片进行专项优化，支持识别艺术字体或背景复杂的文字内容。\n",
    "* 请求URL\n",
    "> https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d4fd37fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和 url/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，要求base64编码和urlencode...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和 image/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pdf_file</td>\n",
       "      <td>和 image/url 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>pdf_file_num</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>detect_direction</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>detect_language</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测语言，默认不检测。当前支持（中文、英语、日语、韩语）</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 参数                  是否必选      类型       可选值范围  \\\n",
       "0             image    和 url/pdf_file 三选一  string           -   \n",
       "1               url  和 image/pdf_file 三选一  string           -   \n",
       "2          pdf_file       和 image/url 三选一  string           -   \n",
       "3      pdf_file_num                     否  string           -   \n",
       "4  detect_direction                     否  string  true/false   \n",
       "5   detect_language                     否  string  true/false   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，要求base64编码和urlencode...  \n",
       "1  图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过4...  \n",
       "2  PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...  \n",
       "3  需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...  \n",
       "4  是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...  \n",
       "5                     是否检测语言，默认不检测。当前支持（中文、英语、日语、韩语）  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/Sk3h7xyad')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a3ee3c18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '楼梯间'}, {'words': 'STAIR ROOM '}, {'words': 'K3-F3-B '}], 'words_result_num': 3, 'log_id': 1513494738470628008}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('楼梯.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f334429",
   "metadata": {},
   "source": [
    "## 二维码识别\n",
    "* 接口描述\n",
    "> 对图片中的二维码、条形码进行检测和识别，返回存储的文字信息。\n",
    "* 请求URL\n",
    "> https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3b137f57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，需去掉编码头（data:image/jp...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      参数       是否必选      类型 可选值范围  \\\n",
       "0  image    和url二选一  string     -   \n",
       "1    url  和image二选一  string     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，需去掉编码头（data:image/jp...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/qk3h7y5o7')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "232371e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['ocr文字识别'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513494788332436149}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('二维码.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "008dbc36",
   "metadata": {},
   "source": [
    "## 身份证识别\n",
    "* 接口描述\n",
    "> 支持对二代居民身份证正反面所有8个字段进行结构化识别，包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限，识别准确率超过99%；同时支持身份证正面头像检测，并返回头像切片的base64编码及位置信息。同时，支持对用户上传的身份证图片进行图像质量和风险检测，是否存在正反颠倒、模糊、欠曝、过曝等质量问题，可识别图片是否为复印件或临时身份证，是否被翻拍或编辑，是否存在四角不完整、头像或关键字段被遮挡。\n",
    "* 请求URL\n",
    "> https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8d3e1fe2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，要求base64编码和urlencode...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>id_card_side</td>\n",
       "      <td>是</td>\n",
       "      <td>string</td>\n",
       "      <td>front/back</td>\n",
       "      <td>-front：身份证含照片的一面-back：身份证带国徽的一面自动检测身份证正反面，如果传参...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>detect_direction</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>detect_risk</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)检测功能，默认...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>detect_quality</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否开启身份证质量类型(边框/四角不完整、头像或关键字段被遮挡/马赛克)检测功能，默认不开启...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>detect_photo</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测头像内容，默认不检测。可选值：true-检测头像并返回头像的 base64 编码及位置信息</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>detect_card</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测身份证进行裁剪，默认不检测。可选值：true-检测身份证并返回证照的 base64 ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 参数       是否必选      类型       可选值范围  \\\n",
       "0             image    和url二选一  string           -   \n",
       "1               url  和image二选一  string           -   \n",
       "2      id_card_side          是  string  front/back   \n",
       "3  detect_direction          否  string  true/false   \n",
       "4       detect_risk          否  string  true/false   \n",
       "5    detect_quality          否  string  true/false   \n",
       "6      detect_photo          否  string  true/false   \n",
       "7       detect_card          否  string  true/false   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，要求base64编码和urlencode...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2  -front：身份证含照片的一面-back：身份证带国徽的一面自动检测身份证正反面，如果传参...  \n",
       "3  是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...  \n",
       "4  是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)检测功能，默认...  \n",
       "5  是否开启身份证质量类型(边框/四角不完整、头像或关键字段被遮挡/马赛克)检测功能，默认不开启...  \n",
       "6  是否检测头像内容，默认不检测。可选值：true-检测头像并返回头像的 base64 编码及位置信息  \n",
       "7  是否检测身份证进行裁剪，默认不检测。可选值：true-检测身份证并返回证照的 base64 ...  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/rk3h7xzck')[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7d87d782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 455, 'left': 701, 'width': 229, 'height': 130}, 'words': '常涛'}, '民族': {'location': {'top': 683, 'left': 1210, 'width': 87, 'height': 104}, 'words': '汉'}, '住址': {'location': {'top': 1088, 'left': 686, 'width': 1070, 'height': 249}, 'words': '河北省邯郸市肥乡县肥乡镇'}, '公民身份号码': {'location': {'top': 1598, 'left': 1103, 'width': 1395, 'height': 134}, 'words': '411526199706013217'}, '出生': {'location': {'top': 875, 'left': 689, 'width': 850, 'height': 106}, 'words': '19970601'}, '性别': {'location': {'top': 683, 'left': 714, 'width': 93, 'height': 106}, 'words': '男'}}, 'words_result_num': 6, 'image_status': 'normal', 'idcard_number_type': 1, 'log_id': 1513494860338694486}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('身份证.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b21bbb5",
   "metadata": {},
   "source": [
    "## 增值税发票识别\n",
    "* 接口描述\n",
    "> 支持对增值税普票、专票、卷票、电子发票、区块链发票的所有字段进行结构化识别，包括发票基本信息、销售方及购买方信息、商品信息、价税信息等，其中五要素识别准确率超过 99.9%； 同时，支持对增值税卷票的 21 个关键字段进行识别，包括发票类型、发票代码、发票号码、机打号码、机器编号、收款人、销售方名称、销售方纳税人识别号、开票日期、购买方名称、购买方纳税人识别号、项目、单价、数量、金额、税额、合计金额(小写)、合计金额(大写)、校验码、省、市，四要素平均识别准确率可达95%以上。\n",
    "* 请求URL\n",
    "> https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "fd2bf25b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和 url/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，要求base64编码和urlencode...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和 image/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pdf_file</td>\n",
       "      <td>和 image/url 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>pdf_file_num</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>type</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>normal/roll</td>\n",
       "      <td>进行识别的增值税发票类型，默认为 normal，可缺省- normal：可识别增值税普票、专...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             参数                  是否必选      类型        可选值范围  \\\n",
       "0         image    和 url/pdf_file 三选一  string            -   \n",
       "1           url  和 image/pdf_file 三选一  string            -   \n",
       "2      pdf_file       和 image/url 三选一  string            -   \n",
       "3  pdf_file_num                     否  string            -   \n",
       "4          type                     否  string  normal/roll   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，要求base64编码和urlencode...  \n",
       "1  图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过4...  \n",
       "2  PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...  \n",
       "3  需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...  \n",
       "4  进行识别的增值税发票类型，默认为 normal，可缺省- normal：可识别增值税普票、专...  "
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/nk3h7xy2t')[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "53ba5e85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 1513501503875621086, 'error_msg': 'image size error', 'error_code': 216202}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('发票2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = access_token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78834694",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
